package com.wgchao.amc.mapstruct.mapper.expand;

import com.wgchao.amc.entity.base.SysBaseAgency;
import com.wgchao.amc.entity.base.SysBaseRole;
import com.wgchao.amc.entity.base.SysBaseUser;
import com.wgchao.amc.mapstruct.DTOMapper;
import com.wgchao.amc.mapstruct.dto.SysBaseRoleDTO;
import com.wgchao.amc.mapstruct.mapper.SysBaseAgencyMapper;
import com.wgchao.amc.mapstruct.mapper.SysBaseRoleMapper;
import com.wgchao.amc.mapstruct.mapper.SysBaseUserMapper;
import com.wgchao.amc.repository.base.SysBaseAgencyRepository;
import com.wgchao.amc.repository.base.SysBaseUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @ClassName: SysBaseRoleMapperExpand
 * @author: wgchao
 * @createTime: 2019/10/29 9:20 AM
 * @Description: TODO
 */
@Component
public class SysBaseRoleMapperExpand implements DTOMapper<SysBaseRole, SysBaseRoleDTO>, CommonExpand<SysBaseRoleDTO>{
    @Autowired
    private SysBaseRoleMapper roleMapper;
    @Autowired
    private SysBaseUserMapper userMapper;
    @Autowired
    private SysBaseUserRepository userRepository;
    @Autowired
    private SysBaseAgencyRepository agencyRepository;
    @Autowired
    private SysBaseAgencyMapper agencyMapper;

    @Override
    public SysBaseRole toEntity(SysBaseRoleDTO sysBaseRoleDTO) {
        return roleMapper.toEntity(sysBaseRoleDTO);
    }

    @Override
    public SysBaseRoleDTO toDTO(SysBaseRole sysBaseRole) {
        return roleMapper.toDTO(sysBaseRole);
    }

    @Override
    public List<SysBaseRole> toEntity(List<SysBaseRoleDTO> sysBaseRoleDTOS) {
        return roleMapper.toEntity(sysBaseRoleDTOS);
    }

    @Override
    public List<SysBaseRoleDTO> toDTO(List<SysBaseRole> sysBaseRoles) {
        return roleMapper.toDTO(sysBaseRoles);
    }

    @Override
    public SysBaseRoleDTO getCURDUserInfo(SysBaseRoleDTO sysBaseRoleDTO) {
        if(sysBaseRoleDTO.getCreateUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(sysBaseRoleDTO.getCreateUser());
            if(userOptional.isPresent()){
                sysBaseRoleDTO.setCreateUserInfo(userMapper.toDTO(userOptional.get()));
            }
        }
        if(sysBaseRoleDTO.getLastUpdateUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(sysBaseRoleDTO.getLastUpdateUser());
            if(userOptional.isPresent()){
                sysBaseRoleDTO.setLastUpdateUserInfo(userMapper.toDTO(userOptional.get()));
            }
        }
        if(sysBaseRoleDTO.getDelUser() != null){
            Optional<SysBaseUser> userOptional = userRepository.findById(sysBaseRoleDTO.getDelUser());
            if(userOptional.isPresent()){
                sysBaseRoleDTO.setDelUserInfo(userMapper.toDTO(userOptional.get()));
            }
        }
        return sysBaseRoleDTO;
    }

    public SysBaseRoleDTO getAgencyInfo(SysBaseRoleDTO roleDTO){
        Optional<SysBaseAgency> agencyOptional = agencyRepository.findById(roleDTO.getAgencyCode());
        if(agencyOptional.isPresent()){
            roleDTO.setAgencyInfo(agencyMapper.toDTO(agencyOptional.get()));
        }
        return roleDTO;
    }
}
